<?php
namespace app\admin\controller;
use think\Request;

/**
 * 新闻分类管理
 */
class Ncategory extends Base
{
	public $req;
	protected $NewsCategory;

	function __construct()
	{
		parent::__construct();
		$this->req = Request::instance();
		$this->NewsCategory = model('NewsCategory');

        // 判断当前控制器, 使相应导航高亮
		$con = __CLASS__;
        $arr = explode('\\', $con);
        $this->assign('_class', strtolower($arr[3]));
	}

	/**
	 * 新闻分类
	 */
	public function categoryList()
	{
		if ($this->req->isPost()) {
			$cList = $this->NewsCategory->select();
			return layuiJson(0, '', count($cList), $cList);

		} else {
			return $this->fetch();
		}
	}

	/**
	 * 修改分类信息
	 */
	public function editCategoryInfo()
	{
		$data = input('post.');
		$res = $this->NewsCategory->save(
			[
				$data['field']=>$data['val']
			],
			[
				'id'=>$data['id']
			]
		);
		if (!$res) {
			return show(1, '更新失败');
		}
		return show(0, '更新成功');
	}

	/**
	 * 添加分类
	 */
	public function addCategory() {
		$data = input('post.');
		$res = $this->NewsCategory->create(['name'=>$data['category_name']]);
		if (!$res) {
			return show(1, '更新失败');
		}
		return show(0, '更新成功');
	}
}
